<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模型名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          placeholder="请输入模型名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主题标签" prop="tag">
        <el-input
          v-model="queryParams.tag"
          placeholder="请输入主题标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模型描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入模型描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createPerson">
        <el-input
          v-model="queryParams.createPerson"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新人" prop="updatePerson">
        <el-input
          v-model="queryParams.updatePerson"
          placeholder="请输入更新人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['etl-model:model:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['etl-model:model:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['etl-model:model:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['etl-model:model:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="modelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模型名称" align="center" prop="modelName" width="200"/>
      <el-table-column label="主题标签" align="center" prop="tag" width="200"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="200" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="200"/>
      <el-table-column label="模型描述" align="center" prop="description" width="250"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleFieldsConfig(scope.row)"
          >属性列表配置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-coin"
            @click="handleCreateTable(scope.row)"
          >生成物理模型</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-grid"
            @click="handleTableDataEdit(scope.row)"
          >编辑数据</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据模型管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="height: 68vh; width: 100%; overflow: auto">
            <el-row>
               <el-col :span="12">
                 <el-form-item label="模型名称" prop="modelName">
                    <el-input v-model="form.modelName" placeholder="请输入模型名称" />
                 </el-form-item>
               </el-col>
               <el-col :span="12">
                 <el-form-item label="主题标签" prop="tag">
                    <el-select v-model="form.tag" placeholder="请选择主题标签" style="width: 100%">
                      <el-option v-for="dict in tagOptions" :key="dict.dictLabel" :label="dict.fcdictLabelontent" :value="dict.dictLabel"></el-option>
                    </el-select>
                 </el-form-item>
               </el-col>
            </el-row>
            <el-row>
               <el-col :span="24">
                 <el-form-item label="模型描述" prop="description">
                    <el-input v-model="form.description" type="textarea"  placeholder="请输入模型描述" :rows="4"/>
                 </el-form-item>
               </el-col>
            </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 模型属性列表编辑对话框 -->
    <el-dialog :title="fieldsDialogTitle" :visible.sync="openFieldsDialog" width="80%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="height: 68vh; width: 100%; overflow: auto">
          <div class="el-dialog-div" style="height: 68vh; width: 100%; overflow: auto">
            <el-row>
              <el-col :span="24">
                <fieldsPage ref="fieldsPage"></fieldsPage>
              </el-col>
            </el-row>
          </div>
      </el-form>
    </el-dialog>

    <!-- 添加或修改数据模型管理对话框 -->
    <el-dialog :title="dataEditTitle" :visible.sync="openDataEdit" width="80%" append-to-body :close-on-click-modal="false">
      <el-form style="height: 68vh; width: 100%; overflow: auto">
        <el-row>
          <el-col :span="24">
            <dataEditPage ref="dataEditPage"></dataEditPage>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDataEditForm">确 定</el-button>
        <el-button @click="cancelDataEdit">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listModel, getModel, delModel, addModel, updateModel,createTable } from "@/api/etl-model/model";
import fieldsPage from "../fields/index";
import dataEditPage from "../modelData/index";

export default {
  name: "Model",
  components: {
      fieldsPage,
      dataEditPage,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据模型管理表格数据
      modelList: [],
      // 主题标签数据字典
      tagOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示数据编辑层
      openDataEdit:false,
      //是否显示属性列表编辑层
      openFieldsDialog:false,
      //当前主tab页名称
      activeMainTabName: "",
      //模型数据编辑框标题
      dataEditTitle: "模型数据编辑",
      fieldsDialogTitle: "模型属性列表编辑",
      codeDiffTitle: "代码差异对话框",
      //是否显示代码差异对话框
      openCodeDiff:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelName: null,
        tag: null,
        description: null,
        createPerson: null,
        updatePerson: null,
        modelKind:"",
      },
      // 表单参数
      form: {},
      form2: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getDicts("model_tag").then(response => {
      this.tagOptions = response.data;
    });
  },
  methods: {
    iniModelKind(_modelKind){
      this.queryParams.modelKind = _modelKind;
      this.getList();
    },
    /** 查询数据模型管理列表 */
    getList() {
      this.loading = true;
      listModel(this.queryParams).then(response => {
        this.modelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        modelName: null,
        tag: null,
        description: null,
        createTime: null,
        updateTime: null,
        createPerson: null,
        updatePerson: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.activeMainTabName = "baseInfoTab";
      this.form.modelKind = this.queryParams.modelKind;
      this.open = true;
      this.title = "添加数据模型管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.activeMainTabName = "baseInfoTab";
      const id = row.id || this.ids
      getModel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据模型管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateModel(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModel(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除数据模型管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delModel(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('etl-model/model/export', {
        ...this.queryParams
      }, `数据模型列表.xlsx`)
    },
    /** 属性列表配置 */
    handleFieldsConfig(row) {
      this.openFieldsDialog = true;
      var self = this;
      console.log("fieldsPage:"+self.$refs["fieldsPage"]);
      if(self.$refs["fieldsPage"]){
        self.$refs["fieldsPage"].getDataModelFields(row);
      }else{
        setTimeout(function(){
           self.$refs["fieldsPage"].getDataModelFields(row);
        },100 );
      }
    },
    /** 生成物理模型*/
    handleCreateTable(row) {
      createTable(row).then(response => {
         this.msgSuccess("生成物理模型成功");
      });
    },
    /** 确认数据修改 */
    submitDataEditForm(){
       this.openDataEdit = false;
    },
    /** 取消数据修改 */
    cancelDataEdit(){
       this.openDataEdit = false;
    },
    /** 编辑数据 */
    handleTableDataEdit(row){
       this.openDataEdit = true;
       var self = this;
       debugger;
      if(self.$refs["dataEditPage"]){
        self.$refs["dataEditPage"].getList(row);
      }else{
        setTimeout(function(){
           self.$refs["dataEditPage"].getList(row);
        },100 );
      }
    },
    handleMainTabClick(tab) {
      if (tab.name == "baseInfoTab") {
        console.log("切换到数据模型基本信息Tab页");
      } else if (tab.name == "fieldsTab") {
        console.log("切换到数据模型属性列表Tab页");
      }
    },
    handleCodeDiff(){
      this.openCodeDiff = true;
    }
  }
};
</script>
<style>

</style>
